/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemetrosystem.model.Element;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Collections;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import simplemetrosystem.control.ElementFactory;
import simplemetrosystem.control.ElementManager;
import simplemetrosystem.model.Element.Interface.ElementListener;
import simplemetrosystem.model.Element.Interface.HighLightable;
import simplemetrosystem.model.Element.Interface.MetroElement;

/**
 *
 * @author Eric Qian
 */
public class MetroLine
        implements MetroElement, Comparable<MetroLine>, HighLightable, ElementListener {

    /**
     * 
     * @param name
     * @param cName
     * @param status 
     * @param color
     * @param language 
     */
    public MetroLine(String name, String cName, String status, Color color, boolean language) {
        this.name = name;
        this.cName = cName;
        this.status = status;
        this.color = color;
        this.language = language;
        this.highLightFlag = false;
        eUpdate();
    }

    /**
     * 
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 
     * @return
     */
    public String getName() {
        return name;
    }

    /**
     * 
     * @param cName
     */
    public void setChineseName(String cName) {
        this.cName = cName;
    }

    /**
     * 
     * @return
     */
    public String getChineseName() {
        return cName;
    }

    /**
     * 
     * @return
     */
    public String getDisplayName() {
        if (language) {
            return getName();
        } else {
            return getChineseName();
        }
    }

    /**
     * 
     * @return
     */
    public String getStatus() {
        return status;
    }

    /**
     * 
     * @param status
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * 
     * @return
     */
    public Color getColor() {
        return color;
    }

    /**
     * 
     * @param color
     */
    public void setColor(Color color) {
        this.color = color;
    }

    /**
     * 
     * @return
     */
    public int getStationsSize() {
        return stations.size();
    }

    /**
     * 
     * @param i
     * @return
     */
    public MetroStation getStationAt(int i) {
        return stations.get(i);
    }

    /**
     * 
     * @param station
     * @return
     */
    public int getIndexOfStation(MetroStation station) {
        return stations.indexOf(station);
    }

    /**
     * 
     * @return
     */
    public int getEdgesSize() {
        return edges.size();
    }

    /**
     * 
     * @param i
     * @return
     */
    public MetroEdge getEdgeAt(int i) {
        return edges.get(i);
    }

    public boolean isHighLight() {
        return highLightFlag;
    }

    public void setHighLight(boolean lightFlag) {
        this.highLightFlag = lightFlag;
        for (int i = 0; i < stations.size(); i++) {
            stations.get(i).setHighLight(lightFlag);
        }
        for (int j = 0; j < edges.size(); j++) {
            edges.get(j).setHighLight(lightFlag);
        }
    }

    public void drawHighLight(Graphics2D gg, int matrixSize) {
    }

    /**
     * 
     * @param doc
     * @return
     */
    public Element getElement(Document doc) {
        Element lineElement = doc.createElement(ElementFactory.ROOT_LINE);

        Element nameElement = doc.createElement(ElementFactory.LINE_NAM);
        lineElement.appendChild(nameElement);
        Text nameText = doc.createTextNode(name);
        nameElement.appendChild(nameText);

        Element cNameElement = doc.createElement(ElementFactory.LINE_CNAM);
        lineElement.appendChild(cNameElement);
        Text cNameText = doc.createTextNode(cName);
        cNameElement.appendChild(cNameText);

        Element statusElement = doc.createElement(ElementFactory.LINE_STATUS);
        lineElement.appendChild(statusElement);
        Text statusText = doc.createTextNode(status);
        statusElement.appendChild(statusText);

        Element colorElement = doc.createElement(ElementFactory.LINE_COLOR);
        lineElement.appendChild(colorElement);

        Element redElement = doc.createElement(ElementFactory.COLOR_RED);
        colorElement.appendChild(redElement);
        Text redText = doc.createTextNode("" + color.getRed());
        redElement.appendChild(redText);

        Element greenElement = doc.createElement(ElementFactory.COLOR_GREEN);
        colorElement.appendChild(greenElement);
        Text greenText = doc.createTextNode("" + color.getGreen());
        greenElement.appendChild(greenText);

        Element blueElement = doc.createElement(ElementFactory.COLOR_BLUE);
        colorElement.appendChild(blueElement);
        Text blueText = doc.createTextNode("" + color.getBlue());
        blueElement.appendChild(blueText);

        return lineElement;
    }

    @Override
    public String toString() {
        return "<line>" + "\n" +
                "<name>" + name + "</name>" + "\n" +
                "<cName>" + cName + "</cName>" + "\n" +
                "<color>" + "\n" +
                "<red>" + color.getRed() + "</red>" + "\n" +
                "<green>" + color.getGreen() + "</green>" + "\n" +
                "<blue>" + color.getBlue() + "</blue>" + "\n" +
                "</color>" + "\n" +
                "</line>" + "\n";
    }

    public int compareTo(MetroLine o) {
        if (status.equals(STATUS_BUS) || status.equals(STATUS_FOOT)) {
            return 1;
        } else if (o.status.equals(STATUS_BUS) || o.status.equals(STATUS_FOOT)) {
            return -1;
        } else if (this.name.compareTo(o.getName()) < 0) {
            return -1;
        } else if (this.name.compareTo(o.getName()) > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    public void eUpdate() {
        ElementManager manager = ElementManager.getInstance();
        stations = manager.getStationsOfLine(this);
        Collections.sort(stations);
        edges = manager.getEdgesOfLine(this);
        Collections.sort(edges);
    }
    private String name;
    private String cName;
    private String status;
    private Color color;
    private boolean language;
    private boolean highLightFlag;
    private ArrayList<MetroStation> stations;
    private ArrayList<MetroEdge> edges;
    public static final String STATUS_METRO = "metro";
    public static final String STATUS_BUS = "bus";
    public static final String STATUS_FOOT = "foot";
}
